<?php
/**
 * 报单中心申请管理
 *
 *
 *
 *
 * @网店运维提供技术支持 授权请购买shopnc授权
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 */



defined('ByShopWWI') or exit('Access Invalid!');

class apply_formControl extends SystemControl{
    
    const EXPORT_SIZE = 1000;
    public function __construct(){
        parent::__construct();
       
    }

    public function indexOp() {
        $this->apply_formOp();
    }

    /**
     * 报单中心申请管理列表
     */
    public function apply_formOp(){
	Tpl::setDirquna('shop');
        Tpl::showpage('apply_form.list');
    }
    
    
    
    /**
     * 通过申请
     */
    public function check_passOp(){
        $lang   = Language::getLangContent();
        $model_apply = Model('apply_form');
        $applyInfo=$model_apply ->getApplyInfo(['apply_form_id'=>intval($_GET['id'])]);
        if($applyInfo['status']!=0)
        {
             showMessage('操作失败');
             exit;
        }
        $update_array=['status'=>1];
        $result = $model_apply->editApply(array('apply_form_id'=>intval($_GET['id'])),$update_array);
        if ($result){
            
            $update_array=['member_type'=>5];
            Model('member')->editMember(['member_id'=>$applyInfo['member_id']],$update_array);
           
            $url = array(
                array(
                'url'=>'index.php?act=apply_form&op=index',
                'msg'=>'',
                )
            );
            $this->log('审核报单中心申请：通过'.'[ID:'.$_GET['id'].']',1);
            showMessage('操作成功',$url);
        }else {
            showMessage('操作失败');
        }
            
        
    }
    /**
     * 拒绝申请
     */
    public function check_refuseOp(){
        $lang   = Language::getLangContent();
        $model_apply = Model('apply_form');
        $applyInfo=$model_apply ->getApplyInfo(['apply_form_id'=>intval($_GET['id'])]);
        if($applyInfo['status']!=0)
        {
             showMessage('操作失败');
             exit;
        }
        $update_array=['status'=>2];
        $result = $model_apply->editApply(array('apply_form_id'=>intval($_GET['id'])),$update_array);
        if ($result){
            
            $update_array=['member_type'=>4];
            Model('member')->editMember(['member_id'=>$applyInfo['member_id']],$update_array);
            $url = array(
                array(
                'url'=>'index.php?act=apply_form&op=index',
                'msg'=>'',
                )
            );
            $this->log('审核报单中心申请：拒绝'.'[ID:'.$_GET['id'].']',1);
            showMessage('操作成功',$url);
        }else {
            showMessage('操作失败');
        }
            
        
    }
  

    /**
     * 输出XML数据,页面显示
     */
    public function get_xmlOp(){
        
        $model_apply = Model('apply_form');
        $condition = array();
        if ($_POST['query'] != '') {
            $condition[$_POST['qtype']] = array('like', '%' . $_POST['query'] . '%');
        }
        $order = '';
        $param = array('apply_form_id','member_id','member_name','member_truename','remark','apply_time','status');

        if (in_array($_POST['sortname'], $param) && in_array($_POST['sortorder'], array('asc', 'desc'))) {
            $order = $_POST['sortname'] . ' ' . $_POST['sortorder'];
        }
        $page = $_POST['rp'];
        $apply_list = $model_apply->getApplyList($condition,$page, $order);
       
      

        $data = array();
        $data['now_page'] = $model_apply->shownowpage();
        $data['total_num'] = $model_apply->gettotalnum();
        foreach ($apply_list as $value) {
            $param = array();
            $param['operation'] =$value['status'] ==  '0'? "<a class='btn blue' href='index.php?act=apply_form&op=check_pass&id=" . $value['apply_form_id'] . "'><i class='fa fa-pencil-square-o'></i>通过</a><a class='btn blue' href='index.php?act=apply_form&op=check_refuse&id=" . $value['apply_form_id'] . "'><i class='fa fa-pencil-square-o'></i>拒绝</a>":"";
            //$param['operation'] ="<a class='btn blue' href='index.php?act=apply_form&op=check_pass&id=" . $value['apply_form_id'] . "'><i class='fa fa-pencil-square-o'></i>通过</a><a class='btn blue' href='index.php?act=apply_form&op=check_refuse&id=" . $value['apply_form_id'] . "'><i class='fa fa-pencil-square-o'></i>拒绝</a>"; 
            $param['apply_form_id'] = $value['apply_form_id'];
            $param['member_name'] = "<a class='btn blue' href='index.php?act=member&op=member_select&member_id=" . $value['member_id'] . "'>
            		<img src=".getMemberAvatarForID($value['member_id'])." class='user-avatar' onMouseOut='toolTip()' onMouseOver='toolTip(\"<img src=".getMemberAvatarForID($value['member_id']).">\")'>".$value['member_name']."</a>";
            $param['member_truename'] = $value['member_truename'];
            $param['remark'] = $value['remark'];
            $param['apply_time'] = date('Y-m-d H:i:s', $value['apply_time']);
            $param['status'] =$value['status'] ==  '0'? '<span class="yes"><i class="fa fa-check-circle"></i>未审批</span>' :($value['status'] ==  '1'  ? '<span class="yes"><i class="fa fa-check-circle"></i>通过</span>' : '<span class="no"><i class="fa fa-ban"></i>拒绝</span>');
          
          
            $data['list'][$value['apply_form_id']] = $param;
        }
        echo Tpl::flexigridXML($data);exit();
    }

   
    /**
     * csv导出
     */
    public function export_csvOp() {
        $model_apply = Model('apply_form');
        $condition = array();
        $limit = false;
        if ($_GET['id'] != '') {
            $id_array = explode(',', $_GET['id']);
            $condition['member_id'] = array('in', $id_array);
        }
        if ($_GET['query'] != '') {
            $condition[$_GET['qtype']] = array('like', '%' . $_GET['query'] . '%');
        }
        $order = '';
        $param = array();
        if (in_array($_GET['sortname'], $param) && in_array($_GET['sortorder'], array('asc', 'desc'))) {
            $order = $_GET['sortname'] . ' ' . $_GET['sortorder'];
        }
        if (!is_numeric($_GET['curpage'])){
            $count = $model_apply->getApplyCount($condition);
            if ($count > self::EXPORT_SIZE ){   //显示下载链接
                $array = array();
                $page = ceil($count/self::EXPORT_SIZE);
                for ($i=1;$i<=$page;$i++){
                    $limit1 = ($i-1)*self::EXPORT_SIZE + 1;
                    $limit2 = $i*self::EXPORT_SIZE > $count ? $count : $i*self::EXPORT_SIZE;
                    $array[$i] = $limit1.' ~ '.$limit2 ;
                }
                Tpl::output('list',$array);
                Tpl::output('murl','index.php?act=apply_form&op=index');
		Tpl::setDirquna('shop');
                Tpl::showpage('export.excel');
                exit();
            }
        } else {
            $limit1 = ($_GET['curpage']-1) * self::EXPORT_SIZE;
            $limit2 = self::EXPORT_SIZE;
            $limit = $limit1 .','. $limit2;
        }

        $apply_list = $model_apply->getApplyList($condition, null, $order,'*', $limit);
        $this->createCsv($apply_list);
    }
    /**
     * 生成csv文件
     */
    private function createCsv($apply_list) {
        $model_apply = Model('apply_form');
        $data = array();
        foreach ($apply_list as $value) {
            $param = array();
            $param['apply_form_id'] = $value['apply_form_id'];
            $param['member_name'] = $value['member_name'];
	    $param['member_truename'] = $value['member_truename'];
	    $param['remark'] = $value['remark'];
            $param['apply_time'] = date('Y-m-d H:i:s', $value['apply_time']);
            $param['status'] = $value['status'] ==  '0'? '未审批' :($value['status'] ==  '1'  ? '通过' : '拒绝');

        
            $data[$value['apply_form_id']] = $param;
        }

        $header = array(
                'apply_form_id' => '申请ID',
                'member_name' => '会员名称',
		'member_truename' => '真实姓名',
		'remark' => '申请备注',
                'apply_time' => '申请时间',
                'status' => '审核状态',
               
        );
        array_unshift($data, $header);
	$csv = new Csv();
        $export_data = $csv->charset($data,CHARSET,'gbk');
        $csv->filename = $csv->charset('apply_list',CHARSET).$_GET['curpage'] . '-'.date('Y-m-d');
        $csv->export($data);   
    }

}